package com.aquila.datacontrol;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.content.Context;

public class DatabaseTools {

	private final static String DATABASE_PATH = android.os.Environment.getDataDirectory().getAbsolutePath()
			+ "/data/com.aquila.datacontrol/databases"; 

	private static Context context;

	public DatabaseTools(Context context) {
		this.context = context;
	}

	public static void checkMyDatabase() {
		
		try {
		// 获得数据库文件的绝对路径
		String databaseFilename = DATABASE_PATH + "/" +SystemConfig.My_DATABASE_NAME  ;
		File dir = new File(DATABASE_PATH);
		// 如果/sdcard/目录不存在，创建这个目录
		if (!dir.exists()) {
		dir.mkdir();
		} 
		// 如果在/sdcard/目录中不存在tongcheng.db文件，则从res\raw目录中复制这个文件到
		if (!(new File(databaseFilename)).exists()) {
		// 获得封装tongcheng.db文件的InputStream对象
		InputStream is = context.getResources().openRawResource(
				R.raw.bible);
		FileOutputStream fos = new FileOutputStream(databaseFilename);
		byte[] buffer = new byte[81920];
		int count = 0;
		// 开始复制tongcheng.db文件
		while ((count = is.read(buffer)) > 0) {
		fos.write(buffer, 0, count);
		}
		fos.close();
		is.close();
		} else {
		}
		} catch (Exception e) {
		
		}
	}
	
	
	public static void checkDatabase() {

		try {
			// 获得数据库文件的绝对路径
			String databaseFilename = DATABASE_PATH + "/" + SystemConfig.DATABASE_NAME;
			File dir = new File(DATABASE_PATH);
			// 如果/sdcard/目录不存在，创建这个目录
			if (!dir.exists()) {
				dir.mkdir();
			} 
			// 如果在/sdcard/目录中不存在tongcheng.db文件，则从res\raw目录中复制这个文件到
			if (!(new File(databaseFilename)).exists()) {
				// 获得封装tongcheng.db文件的InputStream对象
				InputStream is = context.getResources().openRawResource(
						R.raw.bible);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[81920];
				int count = 0;
				// 开始复制tongcheng.db文件
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				fos.close();
				is.close();
			} else {
			}
		} catch (Exception e) {

		}
	}
}
